var chartTime = "";
$(document).ready(function(){
	
	var urlParamObj = getParamobjFromUrl();
	var fieldId = urlParamObj.id;
	$('#comboxFieldName').combobox({
		editable: false,
		url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage',
		valueField: 'id',
		textField: 'name',
		method:'get',
		onSelect: function(record) {
			//alert(record.name + "--" + record.id)
			var paramJson = {"fieldName": record.name};
			$.ajax({
				type: "get",
				url: BASE_URL_FIELD_SERVICE_MONITOR_SEARCH + "/countByFieldNameAndTime" + '?v=' + Date.parse(new Date()),
				data: "fieldName=" + encodeURI(record.name),
				contentType: "application/json",
				success: function(result) {
					if(!result || !result.content || !result.content.length)
					{
						return;
					}
					chartTime = result.content[0].time.split(" ")[0];
					var resultData = {
							time: result.content[0].time.split(" ")[0],
							faildRunData: [],
							practicalRunData: [],
							shouldRunData: []
					};
					for(var i = 0, len = result.content.length; i < len; ++i)
					{
						//alert(result.content[i].time + "---" + result.content[i].interfaceJson);
						var tempItemObj = JSON.parse(result.content[i].interfaceJson);
						resultData.faildRunData.push(tempItemObj.faildRunInterface);
						resultData.practicalRunData.push(tempItemObj.practicalRunInterface);
						resultData.shouldRunData.push(tempItemObj.shouldRunInterface);
					}
					if($('#chart').highcharts())
					{
						$('#chart').highcharts().destroy();
					}
					resultData.fieldName = paramJson.fieldName;
					drawChart(resultData);
				},
				error: function() {
					$.messager.alert('错误','<p>服务异常，获取领域服务监控信息失败，请稍后重试!</p>', "error");
				}
			});
			
		},
		loadFilter:function(data){
			return data.content;
		},
		onLoadSuccess: function() {
			if(fieldId)
			{
				$("#comboxFieldName").combobox("select",fieldId);
			}
			else
			{
				var defaultId = $("#comboxFieldName").combobox("getData")[0].id;
				$("#comboxFieldName").combobox("select",defaultId);
			}
		}
		
	});
	
	/*$(".chart_a").click(function(e) {
		var title = $(this).data("title");
		var fieldId = $('#comboxFieldName').combobox('getValue');
		var fieldName = encodeURI($('#comboxFieldName').combobox('getText'));
		var url=$(this).data("url") + "?fieldId="+fieldId+"&fieldName="+fieldName+"&v=" + Date.parse(new Date());
		var icon=$(this).attr("icon");
		//window.parent.OpenTab(title,url,icon, true);
    });	*/
});

Date.prototype.format = function(partten)
        {
            if(partten ==null||partten=='')
            {
                partten = 'y-m-d'    ;
            }
            var y = this.getYear();
            var m = this.getMonth()+1;
            var d = this.getDate();
            var r = partten.replace(/y+/gi,y);
            r = r.replace(/m+/gi,(m<10?"0":"")+m);
            r = r.replace(/d+/gi,(d<10?"0":"")+d);
            return r; 
        }
function drawChart(dataInfo) {
	
    $('#chart').highcharts({
        chart: {
            type: 'column',
            alignTicks:true
        },
        title: {
            text: '领域服务监控--' + dataInfo.fieldName 
        },
        subtitle: {
            text: dataInfo.time
        },
		credits: {
			enabled: false
		},
		legend: {
			enabled: true
		},
        xAxis: {
			title: {
                text: '第N小时 (小时)'
            },
            labels: {
            	align: "right",
            	x: 28
    		},
            categories: [
                '01',
                '02',
                '03',
                '04',
                '05',
                '06',
                '07',
                '08',
                '09',
                '10',
                '11',
                '12',
				'13',
                '14',
                '15',
                '16',
                '17',
                '18',
                '19',
                '20',
                '21',
                '22',
				'23',
				'24'
            ]
        },
        yAxis: {
        	lineWidth: 1,
            min: 0,
            title: {
                text: '接口执行次数',
                align: 'high',
                rotation: 0,
                offset: -20,
                y: -20
            }
        },
        tooltip: {
			enabled: true,
            headerFormat: '<span style="font-size:10px">第{point.key}小时</span><br />',
            valueSuffix: ' 个',
            /*pointFormat: '' + '',
            footerFormat: '<table><tbody><tr><td style="color:{series.color};padding:0">{series.name}: </td><td style="padding:0"><b>{point.y} 个</b></td></tr></tbody></table>',*/
            shared: false,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.1,
                groupPadding: 0.1,
                borderWidth: 0,
                cursor: "pointer",
                dataLabels: {
                    enabled: true,
                    rotation: -90,
                    color: '#606060',
                    align: 'right',
                    x: 3,
                    y: -22,
                    style: {
                        fontSize: '10px',
                        fontWeight:400
                    },
                	formatter: function() {
                		return this.y == 0? "":this.y;
                	}
                },
                events: {
                	click: function(event) {
                		var title = $(".chart_a").data("title");
                		var fieldId = $('#comboxFieldName').combobox('getValue');
                		var fieldName = encodeURI($('#comboxFieldName').combobox('getText'));
                		var startHour = parseInt(event.point.x,10);
                		var endHour = startHour + 1;
                		var startTime = chartTime + "T" + (startHour < 10? "0"+startHour:startHour) +":00:00";
                		var endTime = chartTime + "T" + (endHour < 10? "0"+endHour+":00:00":(endHour < 24? endHour+":00:00":"23:59:59"));
                		var type = event.point.series.name === "错误执行次数"? "fail":"all";
                		var url=$(".chart_a").data("url") + "?fieldId=" + fieldId + 
                				"&fieldName=" + fieldName + "&startTime=" + startTime +
                				"&endTime=" + endTime + "&type=" + type + "&v=" + Math.random();
                		var icon=$(".chart_a").attr("icon");
                		window.parent.OpenTab(title,url,icon, true);
                	}
                }
            },
            series: {
            	animation:{duration:500}
            }
        },
        series: [/*{
            name: '应运行作业数',
            data: dataInfo.shouldRunData,//[103, 108, 98, 93, 106, 84, 105, 104, 91, 83, 136, 92,49, 71, 136, 129, 174, 176, 135, 148, 216, 194, 95, 54],
			color:"#5ed758"

        }, */{
            name: '实际执行次数',
            data: dataInfo.practicalRunData,//[83, 78, 98, 93, 106, 84, 105, 104, 91, 83, 106, 92,49, 71, 106, 129, 144, 176, 135, 148, 216, 194, 95, 54],
			color:"#04d3fd"
        }, {
            name: '错误执行次数',
            data: dataInfo.faildRunData,//[83, 178, 98, 93, 106, 84, 105, 134, 91, 183, 106, 92,49, 71, 106, 129, 144, 16, 35, 148, 216, 194, 95, 54],
			color:"#ffc658"
        }]
    });
}